package com.vivo.tasks;

import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.Utils.DingConfUtil;
import com.vivo.Utils.SendDingDingMsgUtil;
import com.vivo.dingtalk.entity.DingLinkNotify;
import com.vivo.dingtalk.entity.DingUserInfo;
import com.vivo.dingtalk.service.DingLinkNotifyService;
import com.vivo.dingtalk.service.DingUserInfoService;
import com.vivo.dingtalk.web.vm.LinkMsgVM;
import com.vivo.system.entity.VivoDingConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.List;

@Component
public class TaskLinkNotify {

    @Autowired
    private DingLinkNotifyService dingLinkNotifyService;

    @Autowired
    private SystemService systemService;

    @Autowired
    private DingUserInfoService dingUserInfoService;

    @Autowired
    private DingConfUtil dingConfUtil;

    public void SyncData() {
        handleUnsendList();
    }

    private void handleUnsendList() {
        List<DingLinkNotify> linkNotifyList = dingLinkNotifyService.getUnsendList();

        for (DingLinkNotify linkNotify: linkNotifyList) {
            String phone = linkNotify.getPhone();
            if (StringUtils.isBlank(phone) || phone.length() != 11) {
                linkNotify.setErrcode("-2");
                linkNotify.setErrmsg("手机号码有问题！");
                saveLinkNotify(linkNotify);
                continue;
            }

            User user = systemService.getUserByLoginName(phone);
            if (user == null || user.getLoginFlag().equals("0")) {
                linkNotify.setErrcode("-3");
                linkNotify.setErrmsg("找不到用户或者用户已经停用");
                saveLinkNotify(linkNotify);
                continue;
            }

            DingUserInfo dingUserInfo = dingUserInfoService.getByMobile(phone);
            if (dingUserInfo == null) {
                System.out.println("找不到对应的用户钉钉登陆信息！请提醒其进行钉钉登陆操作！");
                linkNotify.setErrcode("-4");
                linkNotify.setErrmsg("找不到对应的用户钉钉登陆信息！请提醒其进行钉钉登陆操作！");
                saveLinkNotify(linkNotify);
                continue;
            }

            VivoDingConf dingConf = dingConfUtil.getDingConf(dingUserInfo.getDingCompId());
            if (dingConf == null) {
                System.out.println("找不到对应的钉钉配置信息！请及时联系管理员！");
                linkNotify.setErrcode("-5");
                linkNotify.setErrmsg("找不到对应的钉钉配置信息！请及时联系管理员！");
                saveLinkNotify(linkNotify);
                continue;
            }

            // 发送通知
            notifyMsg(linkNotify, dingConf, dingUserInfo);
        }
    }

    private void saveLinkNotify(DingLinkNotify linkNotify) {
        try {
            dingLinkNotifyService.save(linkNotify);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void notifyMsg(DingLinkNotify linkNotify, VivoDingConf dingConf, DingUserInfo dingUserInfo) {
        LinkMsgVM linkMsgVM = new LinkMsgVM();
        linkMsgVM.setAgentId(Long.parseLong(dingConf.getAgentId()));
        linkMsgVM.setAccessToken(dingConf.getAccessToken());
        linkMsgVM.setTitle(linkNotify.getTitle());
        linkMsgVM.setContent(linkNotify.getContent());

        String url = "http://vivo.jsbstx.com/" + linkNotify.getUrl().trim();
        linkMsgVM.setUrl(url);

        linkMsgVM.setDingUserId(dingUserInfo.getDingUserId());
        try {
            OapiMessageCorpconversationAsyncsendV2Response rsp = SendDingDingMsgUtil.sendWorkMsg(linkMsgVM);
            linkNotify.setErrcode(rsp.getErrorCode());
            linkNotify.setErrmsg(rsp.getErrmsg());
        } catch (Exception e) {
            e.printStackTrace();
            linkNotify.setErrcode("-1");
            linkNotify.setErrmsg(e.getMessage());
        }

        saveLinkNotify(linkNotify);
    }

    public static void main(String[] args) throws Exception {
        String url = "http://vivo.jsbstx.com/" + "WebReport/ReportServer?formlet=订货_客户库存上下限统计(新).frm&op=h55";
        System.out.println(url);
    }

}
